package com.songcha.library_common.ui.view

import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.songcha.library_common.R
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.StringUtil
import com.songcha.library_common.util.px2sp
import com.songcha.library_common.util.sp2px

//the view of decimal point less than integer
class SmallDecimalView
@JvmOverloads
constructor(
    context:Context,
    attrs:AttributeSet?=null,
    defStyle:Int=0,
    defRes:Int=0
): ConstraintLayout(context,attrs,defStyle,defRes)
{
    private var mIntegerTextSize=18f.sp2px()
    private var mIntegerTextColor= Color.RED
    private var mDecimalTextSize=14f.sp2px()
    private var mDecimalTextColor= Color.RED
    private var mNumber=0f
    private var mTvInteger:TextView?=null
    private var mTvDecimal:TextView?=null

    init {
        initView(context,attrs)
    }

    private fun initView(context:Context, attrs:AttributeSet?){
        val lp=ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)
        layoutParams=lp

        if(attrs!=null){
            val ta=context.obtainStyledAttributes(attrs, R.styleable.SmallDecimalView)
            mIntegerTextSize=ta.getDimension(R.styleable.SmallDecimalView_integer_text_size,18f.sp2px())
            mIntegerTextColor=ta.getColor(R.styleable.SmallDecimalView_integer_text_color,Color.RED)
            mDecimalTextSize=ta.getDimension(R.styleable.SmallDecimalView_decimal_text_size,14f.sp2px())
            mDecimalTextColor=ta.getColor(R.styleable.SmallDecimalView_decimal_text_color,Color.RED)
            mNumber=ta.getFloat(R.styleable.SmallDecimalView_decimal_number,0f)
            ta.recycle()
        }

        mTvInteger= TextView(context,attrs)
        mTvInteger!!.textSize=mIntegerTextSize.px2sp()
        mTvInteger!!.setTextColor(mIntegerTextColor)
        mTvInteger!!.id=generateViewId()
        mTvInteger!!.setTypeface(mTvInteger!!.typeface, Typeface.BOLD)
        val tvIntegerLp=LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        tvIntegerLp.leftToLeft=LayoutParams.PARENT_ID
        tvIntegerLp.topToTop=LayoutParams.PARENT_ID
        mTvInteger!!.layoutParams=tvIntegerLp

        mTvDecimal=TextView(context,attrs)
        mTvDecimal!!.textSize=mDecimalTextSize.px2sp()
        mTvDecimal!!.setTextColor(mDecimalTextColor)
        mTvDecimal!!.setTypeface(mTvDecimal!!.typeface, Typeface.BOLD)
        val tvDecimalViewLp=LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        tvDecimalViewLp.leftToRight=mTvInteger!!.id
        tvDecimalViewLp.baselineToBaseline=mTvInteger!!.id

        mTvDecimal!!.layoutParams=tvDecimalViewLp

        updateText()

        addView(mTvInteger)
        addView(mTvDecimal)
    }

    fun updateText(){
        if(mNumber==0f){
            mTvInteger?.text=""
            mTvDecimal?.text=""
            return
        }
        val str = StringUtil.float2PlainString(mNumber)
        var index = str.indexOf(".")
        if(index<0){
            mTvInteger?.text=str
            mTvDecimal?.text=""
            return
        }
        try{
            val i=str.substring(0,index)
            val d=str.substring(index,str.length)
            if(i.isNotBlank())
                mTvInteger?.text=i
            if(d.isNotBlank())
                mTvDecimal?.text=d
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    fun setNumber(number:Float){
        mNumber=number
        updateText()
    }
}